#include "usb.h"
const int idVendor = 0x1D50;
const int idProduct = 0x604F;

const  int  Endpoint = 2;
const  int  Timeout = 10;

usb_dev_handle *dev;

usb_dev_handle *open_dev(void)
{
	struct usb_bus *bus;
	struct usb_device *dev;

	for (bus = usb_get_busses(); bus; bus = bus->next)
	{
		for (dev = bus->devices; dev; dev = dev->next)
		{
			if (dev->descriptor.idVendor == idVendor
				&& dev->descriptor.idProduct == idProduct)
			{
				return usb_open(dev);
			}
		}
	}
	return NULL;
}


bool
initialise()
{
	dev = NULL; /* the device handle */
	char tmp[64];
	int ret;
	void* async_read_context = NULL;
	void* async_write_context = NULL;



	usb_init(); /* initialize the library */
	usb_find_busses(); /* find all busses */
	usb_find_devices(); /* find all connected devices */

	if (!(dev = open_dev()))
	{
		//printf("error opening device: \n%s\n", usb_strerror());
		return false;
	}
	else
	{
		//printf("success: device %04X:%04X opened\n", MY_VID, MY_PID);
	}

//usb_set_configuration

	if (usb_claim_interface(dev, 0) < 0)
	{
		//printf("error claiming interface #%d:\n%s\n", MY_INTF, usb_strerror());
		usb_close(dev);
		return 0;
	}
	else
	{
		//printf("success: claim_interface #%d\n", MY_INTF);
	}

/*
	ret = usb_control_msg(dev, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
		14,
		2,
		MY_INTF,  
		tmp, 1, 1000);
*/
	return true;
}


void sendSetting( char * _buff, unsigned  _size )
{
	volatile int result;
	if ( dev != NULL )
		_size = usb_interrupt_write(dev, Endpoint, _buff, _size,Timeout);
}
